package com.jia.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;
import org.springframework.core.env.Profiles;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.service.VendorExtension;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

import java.util.ArrayList;

@Configuration
@EnableSwagger2 // 开启Swagger2
public class SwaggerConfig {

    // 配置了Swagger的Docket的bean实例
    @Bean
    public Docket docket(Environment environment) {
        // 获取项目的环境
        Profiles profiles = Profiles.of("dev","test");
        boolean flag = environment.acceptsProfiles(profiles); //判断是否是dev，test环境
        return new Docket(DocumentationType.SWAGGER_2)
                .groupName("杜佳佳")
                .apiInfo(apiInfo())
                .enable(flag) //是否开启swagger
                .select()
                // .apis(RequestHandlerSelectors.withClassAnnotation()) //指定要扫描的类上的注解
                .apis(RequestHandlerSelectors.basePackage("com.jia.controller")) // 指定要扫描的包
                // .paths(PathSelectors.ant("/jia/**")) // 过滤什么路径 any
                .build();
    }

    // 配置swagger信息
    private ApiInfo apiInfo() {
        Contact contact = new Contact("杜佳佳", "http://www.yfkdu.cn", "980436918@qq.com");
        return new ApiInfo(
                "杜佳佳的Api 文档",
                "再小的帆也能远航",
                "1.0",
                "urn:tos",
                contact,
                "Apache 2.0",
                "http://www.apache.org/licenses/LICENSE-2.0",
                new ArrayList<>());
    }
}
